package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Permission;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
	public static final String EMPLOYEE_IN_SESSION = "employee_in_session";
	public static final String PERMISSION_IN_SESSION = "permission_in_session";

	private static HttpSession getSession() {
		return ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest().getSession();
	}

	public static void setCurrentUser(Employee current) {
		if (current != null) {
			getSession().setAttribute(EMPLOYEE_IN_SESSION, current);

		} else {
			getSession().invalidate();
		}
	}
	public static Employee getCurrentUser() {

		return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
	}

	public static void setCurrentempPermission(List<Permission> current) {
		if (current != null) {
			getSession().setAttribute(PERMISSION_IN_SESSION, current);

		} else {
			getSession().invalidate();
		}
	}
	public static List<Permission> getCurrentempPermission() {

		return (List<Permission>) getSession().getAttribute(PERMISSION_IN_SESSION);
	}



}